package day06;

import javax.swing.*;
import java.awt.*;

public class Demo04 {
    public static void main(String[] args) throws InterruptedException {
        JFrame f = new JFrame("小球运动");
        f.setSize(300,400);
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BallPanel bp = new BallPanel();
        f.add(bp);
        f.setVisible(true);
        bp.move();
    }
}

class BallPanel extends JPanel {
    int x = 30;
    int y = 30;
    public void paint(Graphics g){
        g.clearRect(0,0,300,400);
        g.fillOval(x,y,20,20);
    }

    public void move() throws InterruptedException {
        while(true){
            y+=2;
            if(y>395){
                y = 0;
            }
            Thread.sleep(100);
            repaint();
        }
    }
}